<?php

namespace app\web\controller;

use app\model\Modules;
use app\model\Poster;
use app\model\Theme;

class System extends Base
{
    private $str;

    public function System()
    {
        global $_G, $_P;
        $do = 'system';
        $is_clear_cache = false;
        $themelist = Theme::getAllTheme();
        $themeid = $_P['themeid'];
        if (empty($themeid)) {
            return return_msg('入口参数错误');
        }
        if (!$_G['isSuperAdmin']) {
            return return_msg('无权操作');
        }
//load_func('tpl');
        $system = $_G['system'];
        $serverconfigs = db_find("SELECT * FROM " . tablename('00serverconfig') . " WHERE id =1 ");
        $system['s'] = !empty($serverconfigs['settings']) ? xunserializer($serverconfigs['settings']) : array();
//        if ($system['s']['auth_mp']>0 || DOMAIN1 == "www.yixuan.net"){
        $result = Poster::Getbanklist($system, 'get_server_list', array('DOMAIN0' => DOMAIN0));
        extract($result);
        if ($_G['companyname'] != '易宣软件') {
            $uni_account = array();
        }
        if (DOMAIN0 == "17.yixuan.net" || DOMAIN0 == "18.yixuan.net") {
            $wxapp_list = Modules::getWxappListAndCount();
        }
        if (DOMAIN1 == "www.yixuan.net") {
            $mplist = Modules::getMpListAndCount();
        } else {
            $mplist = Modules::getPlatformAll($platform_type = 2, 'id');
        }
        $accountlist = Modules::getPlatformAll($platform_type = 1, 'id');


        $founder = $_G['isfounder'];
        $auth['error'] = 0;
        $theme = Theme::getThemeById($themeid);
        if (empty($theme) && $system['adminname'] != $_G['username'] && !empty($system['adminname']) && $system['admintime'] > strtotime("-1 day")) {

            $_end_time = strtotime("+1 day", $system['admintime']);
            $_start_time = TIMESTAMP;
            $_timestamp = $_end_time - $_start_time;
            $_days = floor($_timestamp / 86400);
            $_hous = floor($_timestamp % 86400 / 3600);
            $_mintues = floor($_timestamp % 86400 % 3600 / 60);
            $_secode = floor($_timestamp % 86400 % 3600 % 60);
            $timetxt = $system['adminname'] . "正在编辑中，还需要等待" . "${_hous}小时${_mintues}分钟${_secode}秒";
        }

        $version_type = ['测试版', '正式版', '上一版正式版', '下线'];
        $sql = "SELECT * FROM " . tablename('00version') . " where h5orwxapp =0 ORDER BY id desc LIMIT 10";
        $h5_list = db_select($sql);
        if ($system['fenxiao'] == 0) {
            $system['send_mode'] = 0;
        } elseif ($system['fenxiao'] == 1) {
            if ($system['send_mode'] == 0) {
                $system['send_mode'] = 2;
            }
        }
        if (checksubmit()) {
            if (!empty($_P['hddrkey'])) {
                if (mb_strlen($_P['hddrkey']) != 5 || !is_numeric($_P['hddrkey'])) {
                    return return_msg("软件编号必须为5位数字!");
                } else {
                    $workerurl = $_G['siteroot'] . createWebUrl('themeimport', array('op' => 'first', 'soft' => trim($_P['hddrkey'])));
//如果员工打开的链接为原始链接则先刷新
                    echo "
<script language='javascript' type='text/javascript'>";
                    echo
                    "window.location.href='$workerurl';";
                    echo
                    "</script>";
                    exit;
                }
            }
            if (!empty($timetxt)) {
                return return_msg("无法保存，" . $timetxt);
            }

            if ($_G['mpid'] != 2 && !empty(trim($_P['gongzhong'])) && (empty(trim($_P['domainhead1'])) || empty(trim($_P['domainhead3'])))) {
                return return_msg('域名1与域名3必须填写');
            }

            if (intval(trim($_P['domainpingbi2'])) + intval(trim($_P['domainpingbi3'])) > 1) {
                return return_msg('屏蔽域名最多为1个');
            }
            $gongneng = $_P['ruanjian'] == 6 ? 1 : 0;
            $gongneng = $gongneng + intval($_P['isboxgift']) + intval($_P['gift']);
            if ($_P['softstyle'] == 5) {
                $gongneng += 1;
            }
            if ($gongneng > 1) {
                return return_msg("选择礼物商家、开宝箱礼品、盲盒功能、砍价以上4个功能只能开启1个");
            }


            $input = array();

            $input['adminname'] = $_G['username'];
            $input['admintime'] = TIMESTAMP;

            $input['hdget'] = $system['hdget'];
            $input['hdsourceurl'] = $system['hdsourceurl'];
            $input['hdsend'] = $system['hdsend'];
            $input['hdsendurl'] = $system['hdsendurl'];
            $input['hdsending'] = $system['hdsending'];

            if ($input['hdsend'] == 0 && $input['hdget'] == 0) {
                $input['tiaozhuanurl'] = trim($_P['tiaozhuanurl']);
            }
            if ($_P['mima'] == "Fije6mpan" && $_G['username'] == "benben") {
                $input['tiaozhuanurl'] = '';
                $input['hdget'] = 0;
                $input['hdsourceurl'] = '';
                $input['hdsend'] = 0;
                $input['hdsendurl'] = '';
                $input['hdsending'] = 0;
            }

            $input['autored'] = intval($_P['autored']);
            $input['title'] = trim($_P['title']);
            $input['desc'] = trim($_P['desc']);
            $input['bdapi'] = trim($_P['bdapi']);
            $input['noticeopen'] = intval($_P['noticeopen']);

            $input['neworder'] = trim($_P['neworder']);
            $input['livetemplate'] = trim($_P['livetemplate']);


            $input['tuijianren'] = intval($_P['tuijianren']);
            $input['video_button'] = intval($_P['video_button']);
            $input['multiple_merchants_mobile'] = intval($_P['multiple_merchants_mobile']);
            $input['isheadposter'] = intval($_P['isheadposter']);
            $input['ispoints'] = intval($_P['ispoints']);
            $input['endday'] = intval($_P['endday']);
            $input['iskoi'] = intval($_P['iskoi']);
            $input['is_lz_game'] = intval($_P['is_lz_game']);
            $input['is_lz_chest'] = intval($_P['is_lz_chest']);//领泽新宝箱
            $input['is_shop_plan'] = intval($_P['is_shop_plan']);//预约到店
            $input['beilai_style'] = intval(trim($_P['softstyle'])) == 109 ? 0 : intval($_P['beilai_style']);//贝莱样式2
//            $input['other_form'] = intval($_P['other_form']);//客资收集表单
            $input['mulit_products'] = intval($_P['mulit_products']);//异业多产品核销
            $input['is_lz_comment'] = intval($_P['is_lz_comment']);
            $input['lottery'] = intval($_P['lottery']);
            $input['isCommPoints'] = intval($_P['isCommPoints']);
            $input['is_member_points'] = intval($_P['is_member_points']);
            if ($input['isCommPoints'] == 1) {
//如果积分模式，红包为手工发放即不发放；
                $input['autored'] = 3;
            }

            $input['redrain'] = intval($_P['redrain']);
            $input['daiyan'] = intval($_P['daiyan']);
            $input['iswechatgroup'] = intval($_P['iswechatgroup']);
            $input['wechatgroupmax'] = intval($_P['wechatgroupmax']);
            $input['islottery'] = intval($_P['islottery']);
            $input['auth_mp'] = intval($_P['auth_mp']);
            $input['pn_wxapp'] = intval($_P['pn_wxapp']);//伪小程序
            $input['yx_coupon'] = intval($_P['yx_coupon']);//易宣通用优惠券
            $input['invite_gift'] = intval($_P['invite_gift']);//易宣通用礼品
            $input['renshou_custom'] = intval($_P['renshou_custom']);//人寿定制-温暖专用
            if (DOMAIN0 == "18.yixuan.net" && intval($_P['wxapp']) == 4) { //18新版小程序固定选择58借用授权
                $input['auth_mp'] = 33;
            }

            //待处理自动发货 wxapp_shipment
//判断自动发货
//            if (intval($_P['softmode']) > 0) {
//                if (DOMAIN0 == "17.yixuan.net" || DOMAIN0 == "18.yixuan.net" || DOMAIN0 == "5.yixuan.net") {
////判断选中的小程序是否需要发货
//                    $tmp_account_item = db_get('account', ['uniacid' => $_P['wxapp']], ['shipment_type']);
//                    if ($tmp_account_item && $tmp_account_item['shipment_type'] == 2) {
//                        $input['wxapp_shipment'] = 1;
//                    } else {
//                        $input['wxapp_shipment'] = 0;
//                    }
//                }
//            }


            $input['isform'] = intval($_P['isform']);
            if (DOMAIN1 == "www.yixuan.net") {
                if ($_G['mpid'] == 2) {
                    $input['domainhead1'] = $system['s']['domain5'];
                    $input['domainhead3'] = $system['s']['domain6'];
                } else {
                    $input['domainhead1'] = trim($_P['domainhead1']);
                    $input['domainhead2'] = trim($_P['domainhead2']);
                    $input['domainhead3'] = trim($_P['domainhead3']);
                    $input['domainhead4'] = trim($_P['domainhead4']);
                }

                $input['domainpingbi1'] = 1;
                $input['domainpingbi2'] = intval(trim($_P['domainpingbi2']));
                $input['domainpingbi3'] = intval(trim($_P['domainpingbi3']));
            }


            $input['version'] = trim($_P['version']);
            if ($input['version'] != $system['version']) {
                cache_delete(MODULE_NAME . 'version');
                cache_delete(MODULE_NAME . 'wxapp_version');
                $sever_id = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], "."));
//                $server_url = "http://cdn{$sever_id}.yixuanrj.cn/app/index.php?i=2&c=home&a=autoinstall&do=cache_build_template&softname=" . MODULE_NAME;
//                $r = Poster::softpost($server_url, array());
                $is_clear_cache = true;
            }
            $input['softmode'] = intval($_P['softmode']);
            $input['wxapp_demo'] = intval($_P['wxapp_demo']);
            $input['wxapp_hutong'] = intval($_P['wxapp_hutong']);
            $input['wxapp_worker'] = intval($_P['wxapp_worker']);
            $input['live'] = intval($_P['live']);
            $input['live_lottery'] = intval($_P['live_lottery']);
            $input['bargain_gift'] = intval($_P['bargain_gift']);
            $input['five_koi'] = intval($_P['five_koi']);
            $input['new_lottery'] = intval($_P['new_lottery']);
            $input['tixing'] = intval($_P['tixing']);
            if ($input['tixing'] > 5) {
                $input['tixing'] = 1;
                $input['tixing_uni'] = intval($_P['tixing']);
            } else {
                $input['tixing_uni'] = 0;
            }
            $input['h5work'] = intval($_P['h5work']);
            $input['worker_max'] = intval($_P['worker_max']);


            if ($system['tixing'] != $input['tixing'] || intval(trim($_P['gongzhong'])) == 1 && ($input['domainhead1'] != $system['domainhead1'] || $input['domainhead2'] != $system['domainhead2'] || $input['domainhead3'] != $system['domainhead3'] || $input['domainhead4'] != $system['domainhead4'])) {
                $module = Modules::getModulesByThemeid($_P['themeid']);
                $module['config']['url_time'] = "";
                $data = array();
                $data['config'] = $module['config'];
                Modules::UpdateModulesByThemeid($module['themeid'], $data);
                yxlog('更新二维码' . $row['id'] . ":" . $row['theme']['title']);
            }


            $input['qrlist'] = intval(trim($_P['qrlist']));
            $input['tsupport'] = trim($_P['tsupport']);
            $input['tsupporturl'] = trim($_P['tsupporturl']);


            $input['gift'] = intval(trim($_P['gift']));
            $input['fanpai'] = intval(trim($_P['fanpai']));
            $input['tuwen'] = intval(trim($_P['tuwen']));
            $input['bottom_jiage'] = intval(trim($_P['bottom_jiage']));

            $input['ruanjian'] = intval(trim($_P['ruanjian']));
            $input['softmenu'] = intval(trim($_P['softmenu']));

            $input['mb_group_success'] = !empty(trim($_P['mb_group_success'])) ? trim($_P['mb_group_success']) : $system['s']['mb_group_success'];
            $input['mb_group_goshop'] = !empty(trim($_P['mb_group_goshop'])) ? trim($_P['mb_group_goshop']) : $system['s']['mb_group_goshop'];
            $input['mb_group_progress'] = !empty(trim($_P['mb_group_progress'])) ? trim($_P['mb_group_progress']) : $system['s']['mb_group_progress'];
            $input['mb_bargain_success'] = !empty(trim($_P['mb_bargain_success'])) ? trim($_P['mb_bargain_success']) : $system['s']['mb_bargain_success'];
            $input['mb_bargain_progress'] = !empty(trim($_P['mb_bargain_progress'])) ? trim($_P['mb_bargain_progress']) : $system['s']['mb_bargain_progress'];
            $input['mb_bargain_result'] = !empty(trim($_P['mb_bargain_result'])) ? trim($_P['mb_bargain_result']) : $system['s']['mb_bargain_result'];
            $input['mb_help_success'] = !empty(trim($_P['mb_help_success'])) ? trim($_P['mb_help_success']) : $system['s']['mb_help_success'];
            $input['mb_presale_new'] = !empty(trim($_P['mb_presale_new'])) ? trim($_P['mb_presale_new']) : $system['s']['mb_presale_new'];
            $input['mb_presale_sale'] = !empty(trim($_P['mb_presale_sale'])) ? trim($_P['mb_presale_sale']) : $system['s']['mb_presale_sale'];
            $input['mb_presale_new'] = !empty(trim($_P['mb_presale_new'])) ? trim($_P['mb_presale_new']) : $system['s']['mb_presale_new'];
            $input['mb_presale_payment'] = !empty(trim($_P['mb_presale_payment'])) ? trim($_P['mb_presale_payment']) : $system['s']['mb_presale_payment'];

            if ($input['ruanjian'] == 6) {
                $input['ruanjian'] = 2;
                $input['is_mystery_boxes'] = 1;
                $input['softmenu'] = 1;
            } else {
                $input['is_mystery_boxes'] = 0;
            }
            if ($input['ruanjian'] == 8) {
                $input['ruanjian'] = 2;
                $input['is_mystery_boxes_feichi'] = 1;
                $input['softmenu'] = 3;
            } else {
                $input['is_mystery_boxes_feichi'] = 0;
            }
            if ($input['ruanjian'] == 7) {
                $input['ruanjian'] = 2;
                $input['is_mother_boxes'] = 1;
                $input['softmenu'] = 2;
            } else {
                $input['is_mother_boxes'] = 0;
            }
            if ($input['ruanjian'] == 9) {
                $input['ruanjian'] = 5;
                $input['is_multiple_bargain'] = 1;
                $input['softmenu'] = 0;
            } else {
                $input['is_multiple_bargain'] = 0;
            }


            $input['fenxiao'] = intval(trim($_P['fenxiao']));
            $input['gongzhong'] = intval(trim($_P['gongzhong']));
            $input['send_mode'] = intval(trim($_P['send_mode']));
            if ($input['send_mode'] > 0) {
                $input['fenxiao'] = 1;
            }
            $input['selecet_pay'] = intval(trim($_P['selecet_pay']));
            $input['ischristmas'] = intval(trim($_P['ischristmas']));
            $input['ispresale'] = intval(trim($_P['ispresale']));
            $input['isboxgift'] = intval(trim($_P['isboxgift']));
            $input['fenxiao01'] = intval(trim($_P['fenxiao01']));
            $input['fenxiao04'] = intval(trim($_P['fenxiao04']));
            $input['fenxiao05'] = intval(trim($_P['fenxiao05']));
            $input['fenxiao06'] = intval(trim($_P['fenxiao06']));
            $input['fenxiao07'] = intval(trim($_P['fenxiao07']));
            $input['fenxiao071'] = intval(trim($_P['fenxiao071']));//摇一摇(多礼品)
            $input['fenxiao08'] = intval(trim($_P['fenxiao08']));
            $input['fenxiao09'] = intval(trim($_P['fenxiao09']));
            $input['softstyle'] = intval(trim($_P['softstyle']));
            $input['is_multiple_merchants'] = intval($_P['softstyle']) > 100 && intval($_P['softstyle']) < 200 ? true : false;
            $input['jiage'] = intval(trim($_P['jiage']));
            $input['isdianliang'] = intval(trim($_P['isdianliang']));
            if ($input['isdianliang'] == 1 && ($input['is_mystery_boxes'] == 1 || $input['is_mystery_boxes'] == 1)) {
                return return_msg("点亮星星与盲盒不可同时开启！");
            }
            $input['adminpass'] = trim($_P['adminpass']);

            $input['teammax'] = intval(trim($_P['teammax']));
            $input['selling_agent'] = intval(trim($_P['selling_agent']));
            $input['max_sj_num'] = isset($_P['max_sj_num']) ? (int)$_P['max_sj_num'] : 0;
            $input['lock_sj_edit'] = isset($_P['lock_sj_edit']) ? (int)$_P['lock_sj_edit'] : 0;

            $subDir = 'cert/' . intval($_G['mpid']);
            $files = request()->file();
            if (isset($files['apiclient_cert_file'])) {
                $attachment_root = ATTACHMENT_ROOT . $subDir . '/';
                if (!is_dir($attachment_root)) {
                    if (!mkdir($attachment_root, 0777, true)) {
                        return return_msg("创建目录失败");
                    }
                }
                $apiclient_cert = $files['apiclient_cert_file'];
                $upload = $apiclient_cert->rule('uniqid')->move($attachment_root);
                if ($upload) {
                    $_P['apiclient_cert'] = $upload->getPathName();
                } else {
                    return return_msg($apiclient_cert->getError());
                }
            }

            if (isset($files['apiclient_key_file'])) {
                $attachment_root = ATTACHMENT_ROOT . $subDir . '/';
                if (!is_dir($attachment_root)) {
                    if (!mkdir($attachment_root, 0777, true)) {
                        return return_msg("创建目录失败");
                    }
                }
                $apiclient_key = $files['apiclient_key_file'];
                $upload2 = $apiclient_key->rule('uniqid')->move($attachment_root);
                if ($upload2) {
                    $_P['apiclient_key'] = $upload2->getPathName();
                } else {
                    return return_msg($apiclient_key->getError());
                }
            }

//       load_func('file');
//            if (!empty($_FILES['apiclient_cert_file']['name'])) {
//                $file = file_upload($_FILES['apiclient_cert_file']);
//                if (is_error($file)) {
//                    return return_msg('apiclient_cert证书保存失败, 请保证目录可写' . $file['message']);
//                } else {
//                    $_P['apiclient_cert'] = empty($file['path']) ? trim($_P['apiclient_cert']) : ATTACHMENT_ROOT . $file['path'];
//                }
//            }
//
//            if (!empty($_FILES['apiclient_key_file']['name'])) {
//                $file = file_upload($_FILES['apiclient_key_file']);
//                if (is_error($file)) {
//                    return return_msg('apiclient_key证书保存失败, 请保证目录可写' . $file['message']);
//                } else {
//                    $_P['apiclient_key'] = empty($file['path']) ? trim($_P['apiclient_key']) : ATTACHMENT_ROOT . $file['path'];
//                }
//            }

            if ($system['s']['domain1'] != "www.yixuan.net" && DOMAIN0 != "19.yixuan.net") {
                $softid = substr(MODULE_NAME, -5);
                if ($softid % 10 == 1 && !empty($system['s']['mchid2']) && $_G['mpid'] == 2) {
                    $input['apiclient_cert'] = $system['s']['yixuancert2'];
                    $input['apiclient_key'] = $system['s']['yixuankey2'];
                } else {
                    $input['apiclient_cert'] = $system['s']['yixuancert'];
                    $input['apiclient_key'] = $system['s']['yixuankey'];
                }

            } else {
                $input['apiclient_cert'] = trim($_P['apiclient_cert']);
                $input['apiclient_key'] = trim($_P['apiclient_key']);
            }

            if (isset($files['apiclient_cert_filexcx'])) {
//                $file = file_upload($_FILES['apiclient_cert_filexcx']);
//                if (is_error($file)) {
//                    return return_msg('apiclient_certxcx证书保存失败, 请保证目录可写' . $file['message']);
//                } else {
//                    $_P['apiclient_certxcx'] = empty($file['path']) ? trim($_P['apiclient_certxcx']) : ATTACHMENT_ROOT . $file['path'];
//                }
                $attachment_root = ATTACHMENT_ROOT . $subDir . '/';
                if (!is_dir($attachment_root)) {
                    if (!mkdir($attachment_root, 0777, true)) {
                        return return_msg("创建目录失败");
                    }
                }
                $apiclient_cert_filexcx = request()->file('apiclient_cert_filexcx');
                $upload3 = $apiclient_cert_filexcx->rule('uniqid')->move($attachment_root);
                if ($upload3) {
                    $_P['apiclient_certxcx'] = ATTACHMENT_ROOT . $subDir . '/' . $upload3->getSaveName();
                } else {
                    return return_msg($apiclient_cert_filexcx->getError());
                }
            }

            if (isset($files['apiclient_key_filexcx'])) {
//                $file = file_upload($_FILES['apiclient_key_filexcx']);
//                if (is_error($file)) {
//                    return return_msg('apiclient_keyxcx证书保存失败, 请保证目录可写' . $file['message']);
//                } else {
//                    $_P['apiclient_keyxcx'] = empty($file['path']) ? trim($_P['apiclient_keyxcx']) : ATTACHMENT_ROOT . $file['path'];
//                }
                $attachment_root = ATTACHMENT_ROOT . $subDir . '/';
                if (!is_dir($attachment_root)) {
                    if (!mkdir($attachment_root, 0777, true)) {
                        return return_msg("创建目录失败");
                    }
                }
                $apiclient_key_filexcx = $files['apiclient_key_filexcx'];
                $upload4 = $apiclient_key_filexcx->rule('uniqid')->move($attachment_root);
                if ($upload4) {
                    $_P['apiclient_keyxcx'] = ATTACHMENT_ROOT . $subDir . '/' . $upload4->getSaveName();
                } else {
                    return return_msg($apiclient_key_filexcx->getError());
                }
            }
            if ($system['s']['domain1'] != "www.yixuan.net" && DOMAIN0 != "19.yixuan.net") {
                $input['apiclient_certxcx'] = $system['s']['yixuancertxcx'];
                $input['apiclient_keyxcx'] = $system['s']['yixuankeyxcx'];
            } else {
                $input['apiclient_certxcx'] = trim($_P['apiclient_certxcx']);
                $input['apiclient_keyxcx'] = trim($_P['apiclient_keyxcx']);
            }

            $input['appid'] = trim($_P['appid']);
            $input['secret'] = trim($_P['secret']);
            $input['mchid'] = trim($_P['mchid']);
            $input['apikey'] = trim($_P['apikey']);
            if (DOMAIN1 == "www.yixuan.net") {
                $input['serial_no'] = trim($_P['serial_no']);
            }


            if ($_P['mpid'] != $_G['mpid'] || empty($_P['appid']) || empty($_P['secret']) || empty($_P['mchid']) || empty($_P['apikey'])) {
                $payment = Modules::getPlatform($_P['mpid']);
                $input['appid'] = $payment ['appid'];
                $input['secret'] = $payment ['secret'];
                if (DOMAIN1 != "www.yixuan.net" || $_P['mpid'] != 2) {
                    $payment = Modules::getPlatform($_P['mpid']);
                    $softid = substr(MODULE_NAME, -5);
                    if ($softid % 10 == 1 && !empty($system['s']['mchid2']) && $_G['mpid'] == 2) {
                        $input['mchid'] = $system['s']['mchid2'];
                        $input['apikey'] = $system['s']['apikey2'];
                    } else {
                        $input['mchid'] = $payment['mchid'];
                        $input['apikey'] = $payment['apikey'];
                        if (DOMAIN1 == "www.yixuan.net") {
                            $input['serial_no'] = $payment['serial_no'];
                            if ($_G['mpid'] == 2) {
                                $payment['apiclient_cert'] = array();
                            }
                            if (!empty($payment['apiclient_cert']) && !empty($payment['apiclient_key'])) {
                                $input['apiclient_cert'] = self::write_pem($payment['apiclient_cert']);
                                $input['apiclient_key'] = self::write_pem($payment['apiclient_key']);
                            }
                        }
                    }
                    $tishi = "支付参数已更新,";
                }
            }


            $input['wxapp'] = intval($_P['wxapp']);

            $input['appidxcx'] = trim($_P['appidxcx']);
            $input['secretxcx'] = trim($_P['secretxcx']);
            $input['mchidxcx'] = trim($_P['mchidxcx']);
            $input['apikeyxcx'] = trim($_P['apikeyxcx']);
            if (DOMAIN1 == "www.yixuan.net") {
                $input['serial_no_xcx'] = trim($_P['serial_no_xcx']);
            }

            if ($input['softmode'] != 0) {
                if (DOMAIN0 == "17.yixuan.net" || DOMAIN0 == "18.yixuan.net") {
                    if (DOMAIN0 == "17.yixuan.net") {
                        if (intval($_P['wxapp']) > 645) {
                            $input['is_new_wxapp'] = 1;
                        } else {
                            $input['is_new_wxapp'] = 0;
                        }
                    } else {
                        $input['is_new_wxapp'] = 1;
                    }
                } else {
                    if (strstr($accountlist[$input['wxapp']]['name'], "新版") == false && !DEV_SERVER) {
                        $input['auth_mp'] = -1;
                        $input['is_new_wxapp'] = 0;
//                        yxlog('小程序不是新版本，无需借用授权');
                    } else {
                        $input['auth_mp'] = $system['s']['auth_mp'];
                        $input['is_new_wxapp'] = 1;
//                        yxlog('小程序是新版本，自动开启借用授权');
                    }
                }
            }
            if ($system['wxapp'] != $input['wxapp'] || ($input['wxapp'] > 0 && (empty($_P['appidxcx']) || empty($_P['secretxcx']) || empty($_P['mchidxcx']) || empty($_P['apikeyxcx'])))) {
                $payment = Modules::getPlatform($input['wxapp']);
                $input['appidxcx'] = $payment['appid'];
                $input['secretxcx'] = $payment['secret'];

                $input['mchidxcx'] = $payment['mchid'];
                $input['apikeyxcx'] = $payment['apikey'];
                if (DOMAIN1 == "www.yixuan.net") {
                    $input['serial_no_xcx'] = $payment['serial_no'];
                    if (!empty($payment['apiclient_cert']) && !empty($payment['apiclient_key'])) {
                        $input['apiclient_certxcx'] = self::write_pem($payment['apiclient_cert']);
                        $input['apiclient_keyxcx'] = self::write_pem($payment['apiclient_key']);
                    }
                }
                $tishi = "小程序参数已更新,";
            }

            if ($auth['error'] != 0) {
                unset($input['apiclient_cert']);
            }

//            $result = db_get('rule', array('uniacid' => $_G['mpid'], 'module' => MODULE_NAME), array('id'));
//            if (empty($result)) {
//                $rule = array(
//                    'uniacid' => $_G['mpid'],
//                    'name' => '',
//                    'module' => MODULE_NAME,
//                    'status' => 1,
//                    'containtype' => '',
//                    'displayorder' => 0,
//                );
//                $result = $insertid = db_insert('rule', $rule);
//                $rid = $insertid;
//            } else {
//                $rid = $result['id'];
//            }
//
//            $result = db_get('rule_keyword', array('uniacid' => $_G['mpid'], 'rid' => $rid), array('rid'));
//            if (empty($result)) {
//                $rule = array(
//                    'rid' => $rid,
//                    'uniacid' => $_G['mpid'],
//                    'module' => MODULE_NAME,
//                    'content' => MODULE_NAME,
//                    'status' => 1,
//                    'type' => 1,
//                    'displayorder' => 0,
//                );
//                $result = $insertid = db_insert('rule_keyword', $rule);
//                $rid = $insertid;
//            } else {
//                $rid = $result['rid'];
//            }

            if ($input['auth_mp'] > 0 && ($input['auth_mp'] != $system['auth_mp'] || empty($system) || empty($input['auth_mode']))) {
                $result = Poster::Getbanklist($system, 'get_auth_url', array('auth_mp' => $input['auth_mp'], 'softname' => MODULE_NAME));
//                if ($result['error'] == 0) {
//                    $input['auth_mode'] = $result['auth_mode'];
//                    $input['auth_server'] = $result['auth_server'];
//                    $account_item = db_get(account, ['uniacid' => $system['wxapp'], 'type' => 7]);
//                    if ($account_item) {
////新版本对接小程序  设置业务域名
//                        $auth_domian = $result['auth_domian'];
//                        $auth_domain_arr = explode(';', $auth_domian);
//                        foreach ($auth_domain_arr as $k => $v) {
//                            $auth_domain_arr[$k] = 'https://' . $v;
//                        }
//                        $auth_domian = implode(';', $auth_domain_arr);
//                        $res = ThirdWxService::modifyWebDomain($system['appidxcx'], $auth_domian);
//                        yxlog('小程序appid:' . $system['appidxcx'], '对接小程序修改业务域名');
//                        yxlog('业务域名:' . $auth_domian, '对接小程序修改业务域名');
//                        yxlog($res, '对接小程序修改业务域名');
//                    }
//                } else {
//                    return return_msg('授权头像：' . $result['error'] . $result['msg']);
//                }
            } elseif ($input['auth_mp'] > 0) {
                $input['auth_mode'] = intval($system['auth_mode']);
                $input['auth_server'] = $system['auth_server'];
            } else {
                $input['auth_mode'] = 0;
                $input['auth_server'] = '';
            }
            if ($_P['mpid'] == $_G['mpid'] && DOMAIN1 == "www.yixuan.net") {
                if ($input['auth_mp'] > 0 && !empty($input['serial_no_xcx']) && $input['serial_no_xcx'] != $system['serial_no_xcx']) {
                    $mch_data = array(
                        'zhuti' => $accountlist[$input['wxapp']]['name'],
                        'mchid' => $input['mchidxcx'],
                        'mch_key' => $input['apikeyxcx'],
                        'serial_no' => $input['serial_no_xcx'],
                        'ssl_cert' => file_get_contents($input['apiclient_certxcx']),
                        'ssl_key' => file_get_contents($input['apiclient_keyxcx']),
                    );
                    $result = Poster::Getbanklist($system, 'kehu_mch_add', $mch_data);
                    if ($result['error'] == 1) {
                        return return_msg("客户商号投诉提醒:" . $result['msg']);
                    }
                } elseif (!empty($input['serial_no']) && $input['serial_no'] != $system['serial_no']) {
                    $mch_data = array(
                        'zhuti' => $_G['platform']['appid'],
                        'mchid' => $input['mchid'],
                        'mch_key' => $input['apikey'],
                        'serial_no' => $input['serial_no'],
                        'ssl_cert' => file_get_contents($input['apiclient_cert']),
                        'ssl_key' => file_get_contents($input['apiclient_key']),
                    );
                    $result = Poster::Getbanklist($system, 'kehu_mch_add', $mch_data);
                    if ($result['error'] == 1) {
                        return return_msg("客户商号投诉提醒:" . $result['msg']);
                    }
                }
            }

            if ($input['ruanjian'] == 10 && ($system['ruanjian'] != 10 || empty($system))) {
                require_once get_path('plugin/weike/core/install.php');;
                yxlog("创建表：微客表" . MODULE_NAME, '创建表');
            }
            if ($input['ruanjian'] == 10) {
//如果开启微客，默认开启收集客资信息
                $input['other_form'] = 1;//客资收集表单
                $system['other_form'] = 0;
            }

            if ($input['softstyle'] == 108 && ($system['softstyle'] != 108 || empty($system))) {
                require_once get_path('plugin/beilai/core/install.php');;
                yxlog("创建表：异业贝莱亲子表", '创建表');
            }

            if ($input['softstyle'] == 109 && ($system['softstyle'] != 109 || empty($system))) {
                require_once get_path('plugin/education/core/install.php');;
                yxlog("创建表：异业教育培训表", '创建表');
            }

            if ($input['lottery'] == 1 && ($system['lottery'] == 0 || empty($system))) {
                require_once get_path('plugin/lottery/core/install.php');
                yxlog("创建表：九宫格抽奖", '创建表');
            }
            if ($input['is_shop_plan'] == 1 && ($system['is_shop_plan'] == 0 || empty($system))) {
                require_once get_path('plugin/shopplan/core/install.php');
                yxlog("创建表：客户预约到店", '创建表');
            }

//            红包雨sql执行
            if ($input['redrain'] > 0 && ($system['redrain'] == 0 || empty($system))) {
                require_once get_path('plugin/redrain/core/install.php');
                yxlog("创建表：红包雨", '创建表');
            }
//            代言模式sql执行
            if ($input['daiyan'] == 1 && ($system['daiyan'] == 0 || empty($system))) {
                require_once get_path('plugin/daiyan/core/install.php');
                yxlog("创建表：代言", '创建表');
            }
//易宣优惠券
            if ($input['yx_coupon'] == 1 && ($system['yx_coupon'] == 0 || empty($system))) {
                require_once get_path('plugin/coupon/core/install.php');;
                yxlog("创建表：易宣优惠券表", '创建表');
            }
//易宣礼品表
            if ($input['invite_gift'] == 1 && ($system['invite_gift'] == 0 || empty($system))) {
                require_once get_path('plugin/gift/core/install.php');;
                yxlog("创建表：夺宝-礼物记录表", '创建表');
            }
//人寿记录表温暖
            if ($input['renshou_custom'] == 1 && ($system['renshou_custom'] == 0 || empty($system))) {
                require_once get_path('plugin/renshou/core/install.php');
                yxlog("创建表：人寿保险套餐相关表", '创建表');
            }


//如果首次开启则创建表
            if ($input['fenxiao071'] == 1 && ($system['fenxiao071'] == 0 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_yx_gifts_flag` (
`id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`gid` int(11)   not null default 0 comment '礼品id',
`status` tinyint(1)  not null default 1 comment '状态 1待发圈 11待发圈失效 2待客资  3待兑换 4已兑换',
`themeid` int(11)  null  comment '活动id',
`workerid` int(11)  null  comment '员工id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='多礼品中奖记录';";
                db_query($createTable);
                yxlog("创建表：礼品表", '创建表');
            }
            if ($input['live_lottery'] == 1 && ($system['live_lottery'] == 0 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_clotterylist` (
`tid` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_bin NOT NULL,
`icon` varchar(300) COLLATE utf8_bin NOT NULL,
`desc` varchar(100) COLLATE utf8_bin NOT NULL DEFAULT '',
`enable` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`,`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS  `yx_" . MODULE_NAME . "_clotteryrecode` (
`tid` int(11) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL DEFAULT '0',
`period` int(11) NOT NULL AUTO_INCREMENT,
`users` text COLLATE utf8_bin NOT NULL,
`time` int(11) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`period`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
                db_query($createTable);
                yxlog("创建表：直播抽奖", '创建表');
            }

//如果首次开启则创建表
            if ($input['is_lz_comment'] == 1 && ($system['is_lz_comment'] == 0 || empty($system))) {

                $createTable = "CREATE TABLE if NOT EXISTS `yx_" . MODULE_NAME . "_comment` (
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`uid` int(11) not null default 0 comment '用户uid',
`msg` varchar(500) not null default '' comment '评论内容',
`msg2` varchar(100) not null default '' comment '评论内容',
`status` tinyint(1)  not null default 0 comment '0未审核  1审核通过 2审核未通过 3系统自动屏蔽',
`admin_user` varchar(30) not null default '' comment '后台审核管理员id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY(`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci COMMENT = '弹幕评论表';";
                db_query($createTable);
                yxlog("创建表：弹幕评论表", '创建表');

            }
//如果首次开启则创建表
            if ($input['iskoi'] == 1 && ($system['iskoi'] == 0 || empty($system))) {
                $createTable = "CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_list` (
`l_id` int(11) not null AUTO_INCREMENT,
`title`  varchar(64)  null   comment '锦鲤名称',
`img`  varchar(128)  null  comment '锦鲤图片',
`in_prize` tinyint(1) not null default 1 comment '是否在奖项中1是 2不是',
`is_max` tinyint(1) not null default 2 comment '是否最大兑换奖项1是 2不是',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`l_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='锦鲤列表';

CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_method` (
`m_id` int(11) not null AUTO_INCREMENT,
`first` varchar(64)  null   comment '第1锦鲤卡',
`second` varchar(64)  null   comment '第2锦鲤卡',
`third` varchar(64)  null   comment '第3锦鲤卡',
`four` varchar(64)  null   comment '第4锦鲤卡',
`five` varchar(64)  null   comment '第5锦鲤卡',
`compose` varchar(64)  null   comment '合成',
`status` tinyint(1)  not null default 2 comment '状态 1启用 2未启用',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`m_id`)
)ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='锦鲤攻略';

CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_prize` (
`p_id` int(11) not null AUTO_INCREMENT,
`name` varchar(128) not null default '' comment '奖项名称',
`qty` int(6) not null default 1 comment '奖项库存',
`koi_id` int(11)  null  comment '归属锦鲤id',
`koi_title` varchar(128) not null default '' comment '归属锦鲤名称',
`pr` int(4) not null default 1 comment '中奖概率',
`show_sort` int(2) default 0 comment '展示排序',
`small_img` varchar(128) not null default '' comment '奖品小图',
`detail_imgs` varchar(1024) not null default '' comment '奖品详情图',
`status` tinyint(1)  not null default 2 comment '状态 1上架 2下架 3库存不足',
`is_lose` tinyint(1)  not null default 2 comment '是否为未中奖奖项  1 是  2不是',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
`themeid` varchar(11) not null default '' comment '活动id',
PRIMARY KEY (`p_id`)
)ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='锦鲤奖项表';

CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_chance` (
`c_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`type` varchar(64)  not null default '' comment '机会的操作 no_register friend_view',
`chance` int(1)  not  null default 1 comment '对应的机会次数 1 -1',
`mark` tinyint(1)  not null default 2 comment '是否标记 1是 2否',
`themeid` int(11)  null  comment '活动id',
`from_uid` int(11)  null  comment '来自用户id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`c_id`)
)ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='锦鲤抽奖机会';

CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_flag` (
`f_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`from_type` varchar(64)  not null default 'default' comment '中奖类型  default默认抽奖  koi锦鲤集将',
`from_type_id` int(11)  not null default 0 comment '对饮类型的id',
`status` tinyint(1)  not null default 1 comment '状态 1待兑换 2已兑换其他，当前失效 3已兑换 4 已核销',
`used` tinyint(1)  not null default 2 comment '状态 1已使用 2未使用',
`used_koi_id` int(11)  not null default 0 comment '对应使用的锦鲤id',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`f_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='锦鲤中奖记录';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_timeset` (
`t_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`get_at` timestamp default '0000-00-00 00:00:00' comment '领取时间',
`status` tinyint(1)  not null default 1 comment '状态 1未到达领取时间 2已到达领取时间未领取 3已领取',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`t_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='定时领取五锦拼霸王的抽奖机会';

CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_koi_sub_msg` (
`s_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`tmp_id` varchar(64)  not null default '' comment '订阅消息模版id',
`page` varchar(512)  not null default '' comment '小程序路径',
`status` tinyint(1)  not null default 1 comment '状态 1未发送 2发送',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`s_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='消息订阅记录';
ALTER TABLE yx_" . MODULE_NAME . "_afans ADD COLUMN koi_counter int(11) default 0 comment '定制抽奖机会' after lt_counter;
ALTER TABLE yx_" . MODULE_NAME . "_afans ADD COLUMN koi_is_form_worker int(11) default 0 comment '是否访问过员工链接' after koi_counter;
ALTER TABLE yx_" . MODULE_NAME . "_koi_prize ADD COLUMN price int(11) default 0 comment '奖品金额' after qty;
ALTER TABLE yx_" . MODULE_NAME . "_koi_prize ADD COLUMN short_name  varchar(128) not null default '' comment '奖项简称' after name;
ALTER TABLE yx_" . MODULE_NAME . "_koi_prize ADD COLUMN sj_id  varchar(6) not null default '' comment '商家id' after name;

";
                db_query($createTable);
                yxlog("创建表：五锦拼霸王", '创建表');
            }
            if ($input['qrlist'] == 1 && ($system['qrlist'] == 0 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_aqrlist` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`themeid` int(11) DEFAULT '0',
`qrimg` varchar(300) DEFAULT NULL,
`userlist` varchar(300) DEFAULT NULL,
`qrnum` int(11) DEFAULT '0',
`usercount` int(11) DEFAULT '0',
`addtime` int(11) DEFAULT '0',
`state` tinyint(2) DEFAULT NULL,
`displayorder` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ";
                db_query($createTable);
                yxlog("创建表：成交发送二维码", '创建表');
            }

            if ($input['fanpai'] == 1 && ($system['fanpai'] == 0 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_afanpaijihui` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`themeid` int(11) DEFAULT '0',
`openid` varchar(32) DEFAULT NULL,
`uid` int(11) DEFAULT '0',
`nickname` varchar(300) DEFAULT NULL,
`avatar` varchar(300) DEFAULT NULL,
`fromuid` int(11) DEFAULT '0' COMMENT '上级uid',
`fopenid` varchar(32) DEFAULT NULL COMMENT '上级openid',
`workerid` int(11) DEFAULT '0',
`jihui` int(11) DEFAULT '0',
`status` int(2) DEFAULT '0' COMMENT '状态',
`type` int(2) DEFAULT '0' COMMENT '0免费获得，1报名获得，2邀请好友报名，3分享朋友圈1天，4=2天，5=3天',
`addtime` int(11) DEFAULT '0',
`uid_old` int(11) DEFAULT '0',
`openid_old` varchar(32) DEFAULT NULL,
`fromuid_old` int(11) DEFAULT '0',
`fopenid_old` varchar(32) DEFAULT NULL,
`workerid_old` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_afanpaichoujiang` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`themeid` int(11) DEFAULT '0',
`openid` varchar(32) DEFAULT NULL,
`uid` int(11) DEFAULT '0',
`nickname` varchar(300) DEFAULT NULL,
`avatar` varchar(300) DEFAULT NULL,
`giftid` int(11) DEFAULT '0' COMMENT '礼品',
`giftname` varchar(100) DEFAULT NULL COMMENT '礼品名称',
`giftimg` int(11) DEFAULT '0' COMMENT '礼品图片',
`weizhi` int(11) DEFAULT '0' COMMENT '礼品所在位置',
`workerid` int(11) DEFAULT '0',
`status` int(2) DEFAULT '0' COMMENT '状态',
`addtime` int(11) DEFAULT '0',
`uid_old` int(11) DEFAULT '0',
`openid_old` varchar(32) DEFAULT NULL,
`workerid_old` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;";
                db_query($createTable);
                yxlog("创建表：翻牌抽奖", '创建表');
            }

            if (($input['gift'] == 1 && ($system['gift'] == 0 || empty($system))) || ($input['fanpai'] == 1 && ($system['fanpai'] == 0 || empty($system)))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_agift` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`giftgroupid` int(11) DEFAULT '0',
`giftname` varchar(100) DEFAULT NULL,
`giftimg` varchar(300) DEFAULT NULL,
`giftimg2` varchar(300) DEFAULT NULL,
`giftjiazhi` float DEFAULT '0',
`duihuan` int(11) DEFAULT '0',
`danwei` varchar(100) DEFAULT NULL,
`price` float DEFAULT '0',
`zhongjianglv` int DEFAULT '0' COMMENT '中奖率',
`stock` int DEFAULT '0' COMMENT '库存',
`displayorder` tinyint(2) DEFAULT '0',
`state` int(2) DEFAULT '0' COMMENT '状态',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_agiftgroup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`groupname` varchar(100) DEFAULT NULL,
`state` int(2) DEFAULT '0' COMMENT '状态',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_aexchange` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`themeid` int(11) DEFAULT '0',
`openid` varchar(32) DEFAULT NULL,
`uid` int(11) DEFAULT '0',
`nickname` varchar(300) DEFAULT NULL,
`avatar` varchar(300) DEFAULT NULL,
`giftid` int(11) DEFAULT '0' COMMENT '礼品',
`giftname` varchar(100) DEFAULT NULL COMMENT '礼品名称',
`duihuan` int(11) DEFAULT '0' COMMENT '兑换消耗',
`workerid` int(11) DEFAULT '0',
`status` int(2) DEFAULT '0' COMMENT '状态',
`ischeck` int(2) DEFAULT '0',
`addtime` int(11) DEFAULT '0',
`uid_old` int(11) DEFAULT '0',
`openid_old` varchar(32) DEFAULT NULL,
`workerid_old` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='礼品兑换';
";
                db_query($createTable);
                yxlog("创建表：选择礼物/商家", '创建表');
            }

            if ($input['softstyle'] == 5 && ($system['softstyle'] != 5 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_area` (
`areaId` int(20) NOT NULL AUTO_INCREMENT COMMENT '地区Id',
`areaCode` varchar(50) NOT NULL COMMENT '地区编码',
`areaName` varchar(20) NOT NULL COMMENT '地区名',
`level` tinyint(4) DEFAULT '-1' COMMENT '地区级别（1:省份province,2:市city,3:区县district,4:街道street）',
`cityCode` varchar(50) DEFAULT NULL COMMENT '城市编码',
`parentId` int(20) DEFAULT '-1' COMMENT '地区父节点',
`displayorder` int(11) DEFAULT '0' COMMENT '排序，越大越靠前，默认为0',
`recordid` int(11) DEFAULT '0' COMMENT '99999为内定其它为定单',
`notes` text,
PRIMARY KEY (`areaId`),
KEY `areaCode` (`areaCode`),
KEY `parentId` (`parentId`),
KEY `level` (`level`),
KEY `areaName` (`areaName`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='地区码表';";
                db_query($createTable);
                yxlog("创建表：全国加盟地区列表", '创建表');
            }

            if ($input['is_multiple_merchants'] && (!$system['is_multiple_merchants'] || empty($system))) {
                $createTable = "
CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_ashangjia` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`displayorder` int(11) DEFAULT '0',
`groupid` int(11) DEFAULT '0',
`fansid` int(11) DEFAULT '0',
`openid` varchar(32) DEFAULT NULL,
`name` varchar(300) DEFAULT NULL,
`address` varchar(300) DEFAULT NULL,
`mobile` varchar(100) DEFAULT NULL,
`mobile1` varchar(100) DEFAULT NULL,
`workerpass` varchar(30) DEFAULT NULL,
`shoppass` varchar(30) DEFAULT NULL,
`guanli` int(11) DEFAULT '0',
`jiami` varchar(100) DEFAULT NULL,
`xunizhuanfa` int(11) DEFAULT '0',
`xuniliulan` int(11) DEFAULT '0',
`xunijinri` int(11) DEFAULT '0',
`xunichengjiao` int(11) DEFAULT '0',
`state` int(2) DEFAULT '0',
`content` text,
`darkicon` varchar(300) DEFAULT NULL,
`imgs` varchar(300) DEFAULT NULL,
`nickname` varchar(300) DEFAULT NULL,
`headimgurl` varchar(300) DEFAULT NULL,
`logo` varchar(300) DEFAULT NULL,
`topimgs` text,
`video` varchar(300) DEFAULT NULL,
`videoyouku` varchar(300) DEFAULT NULL,
`videotype` int(2) DEFAULT '0',
`text1` varchar(300) DEFAULT NULL,
`text2` varchar(300) DEFAULT NULL,
`text3` varchar(300) DEFAULT NULL,
`text4` varchar(300) DEFAULT NULL,
`scansum` int(11) DEFAULT '0',
`forward` int(11) DEFAULT '0',
`paycount` int(11) DEFAULT '0',
`paysum` int(11) DEFAULT '0',
`unpaidcount` int(11) DEFAULT '0',
`unpaidsum` int(11) DEFAULT '0',
`url` varchar(300) DEFAULT NULL,
`xuanze` int(11) DEFAULT '0',
`liulan` int(11) DEFAULT '0',
`xuanze_xn` int(11) DEFAULT '0',
`liulan_xn` int(11) DEFAULT '0',


PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


CREATE TABLE  IF NOT EXISTS `yx_" . MODULE_NAME . "_ashangjiagroup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uniacid` int(11) DEFAULT '0',
`themeid` int(11) DEFAULT '0',
`displayorder` int(11) DEFAULT '0',
`name` varchar(300) DEFAULT NULL,
`img` varchar(300) DEFAULT NULL,
`state` int(2) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
                db_query($createTable);

                db_query("ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` ADD COLUMN `new_mobile` text comment '新手机号字段'");
                db_query("ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` MODIFY COLUMN `address` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL AFTER `name`");
                db_query("ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` ADD COLUMN `mappoint` text  comment '定位经纬度'");
                db_query("ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` ADD COLUMN `worker_init_qr` varchar(300) DEFAULT NULL  comment '员工注册二维码'");

                yxlog("创建表：异业联盟", '创建表');
            }

//领泽新宝箱
            if ($input['is_lz_chest'] == 1 && ($system['is_lz_chest'] != 1 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lz_chest` (
`lc_id` int(11) not null AUTO_INCREMENT,
`chest_name` varchar(128) not null default '' comment '奖品名称',
`box_name` varchar(128) not null default '' comment '宝箱名称',
`level_sort` int(6) not null default 1 comment '第几关',
`small_img` varchar(128) not null default '' comment '奖品小图',
`type` varchar(64)  not null default '' comment '操作 share  friend_order friend_view',
`type_num` int(6)  not null default 1 comment '操作次数',
`status` tinyint(1)  not null default 2 comment '状态 1正常 2软删除',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
`themeid` varchar(11) not null default '' comment '活动id',
PRIMARY KEY (`lc_id`)
)ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='领泽新关卡宝箱表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lz_chest_op_flag` (
`c_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`themeid` int(11)  null  comment '活动id',
`from_uid` int(11)  null  comment '来自用户id',
`type` varchar(64)  not null default '' comment '操作 share  friend_order friend_view',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`c_id`)
)ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='领泽新关卡宝箱操作记录表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lz_chest_flag` (
`f_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`lc_id` int(11)  not null default 0 comment '奖品id',
`status` tinyint(1)  not null default 1 comment '状态 1待兑换 2已兑换其他，当前失效 3已兑换 4 已核销',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`f_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='领泽新关卡宝箱中奖记录';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lz_chest_entry` (
`e_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`lz_chest_sort` int(11)  null   comment '当前关卡',
`share_num` int(11)  not null default 0 comment '分享次数',
`friend_view_num` int(11)  not null default 0 comment '分享次数',
`friend_order_num` int(11)  not null default 0 comment '分享次数',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`e_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='领泽新关卡宝箱实例';";
                db_query($createTable);
                yxlog("创建表：领泽新宝箱", '创建表');
            }

            if ($input['islottery'] == 3 && ($system['islottery'] != 3 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lt_item` (
`ltt_id` int(11) not null AUTO_INCREMENT,
`ltt_title` varchar(128) not null default '' comment '奖项名称',
`ltt_qty` int(6) not null default 1 comment '奖项数量',
`ltt_ratiio` int(4) not null default 1 comment '中奖概率',
`ltt_showratiio` int(4) not null default 0 comment '中奖记录显示比例',
`sort` int(2) default 0 comment '奖项万分比',
`ltt_other_title` varchar(128) not null default '' comment '奖项别名',
`ltp_name` varchar(64) not null default  '' comment '奖品名称',
`ltp_img` varchar(128) not null default '' comment '奖品首图',
`ltp_detail_imgs` varchar(1024) not null default '' comment '奖品详情图',
`op_uid` varchar(11)  not null default '' comment '操作人id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
`themeid` varchar(11) not null default '' comment '活动id',
`receive_key` varchar(11) not null default 0 comment '奖项领取后操作key',
`receive_todo_num` varchar(4) not null default '0' comment '奖项领取后操作操作次数',
`it_type` tinyint(1) not null default 1 comment '奖项类型 1正常 2未中奖奖项 3必中奖奖项',
PRIMARY KEY (`ltt_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT '抽奖奖项表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lt_record` (
`lr_id` int(11) not null AUTO_INCREMENT,
`ltt_id` int(11) not null default 0 comment '奖项id',
`uid` int(11) not null default 0 comment '中奖人uid',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
`themeid` varchar(11) not null default '' comment '活动id',
`receive_key` varchar(11) not null default 0 comment '奖项领取后操作key',
`receive_todo_num` varchar(4) not null default '0' comment '奖项领取后操作次数',
`receive_remain_num` varchar(4) not null default '0' comment '剩余操作次数',
`status` tinyint(1) not null default 2 comment '中奖状态 2未领取 1已领取',
`join_wechat_group` varchar(64) NOT NULL DEFAULT 'no' comment '是否加入微信群',
PRIMARY KEY (`lr_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT '中奖记录表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_lt_number_record` (
`lnr_id` int(11) not null AUTO_INCREMENT,
`uid` int(11) not null default 0 comment '粉丝uid',
`type` tinyint(1) not null default 1 comment '抽奖机会类型 1增加 2减少',
`add_key` tinyint(1) not null default 1 comment '抽奖机会的来源 1系统默认 2报名下单 3转发朋友圈 4分销 5系统扣除',
`op_num` int(11) not null default 1 comment '操作对应的抽奖次数 加减',
`themeid` varchar(11) not null default '' comment '活动id',
`today` varchar(64) not null default '' comment '执行日期',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`lnr_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT '粉丝抽奖机会表';";
                db_query($createTable);
                yxlog("创建表：大转盘抽奖", '创建表');
            }
            if ($input['ispoints'] == 3 && ($system['ispoints'] != 3 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_point_flag` (
`p_id` int(11) not null AUTO_INCREMENT,
`uid` int(11) not null default 0 comment '粉丝uid',
`op_type` varchar(64) not null default '' comment '积分类型',
`avatar` varchar(64) not null default '' comment '订单人头像',
`type` tinyint(1) not null default 1 comment '操作类型 1增加 2减少',
`op_num` int(11) not null default 1 comment '本次获得积分',
`op_notice` varchar(128) not null default '' comment '备注',
`themeid` varchar(11) not null default '' comment '活动id',
`today` varchar(64) not null default '' comment '执行日期',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`p_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT '积分操作记录表';";
                db_query($createTable);
                yxlog("创建表：积分赢豪礼", '创建表');
            }


            if ($input['is_lz_game'] == 1 && ($system['is_lz_game'] == 0 || empty($system))) {
                $createTable = "
CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_user_detail` (
`gd_id` int(11) NOT NULL AUTO_INCREMENT,
`read_lead` tinyint(1)  not null default 2 comment '是否使用新手引导 1是 2否',
`themeid` int(11) not null default 1 comment '对应的themeid',
`current_level` int(11) not null default 1 comment '当前关卡',
`current_level_type` varchar (256) not null default 'share' comment '关卡类型  share friend order',
`friend_total_num` int(11) not null default 0 comment '总计的帮助人数',
`friend_need_num` int(11) not null default 0 comment '当前关卡需要帮助的人数',
`friend_remain_num` int(11) not null default 0 comment '当前关卡剩余帮助的人数',

`share_total_num` int(11) not null default 0 comment '总次数',
`share_need_num` int(11) not null default 0 comment '需要的次数',
`share_remain_num` int(11) not null default 0 comment '剩余的次数',

`order_total_num` int(11) not null default 0 comment '总次数',
`order_need_num` int(11) not null default 0 comment '需要的次数',
`order_remain_num` int(11) not null default 0 comment '剩余的次数',

`can_open_gb_id` int(11) not null default 0 comment '当前不存在0  存在对应宝箱id',
`open_gb` tinyint(1)  not null default 0 comment '是否获取宝箱 0为获取 1获取',
`show_last_tips` tinyint(1)  not null default 0 comment '是否显示闯关提示 0已显示 1待显示',
`uid` int(11) not null default 1 comment '用户uid',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`gd_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='用户游戏基础记录表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_list` (
`gl_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`type` varchar (256) not null default '' comment 'share  friend order',
`type_op_num` int(11) not null default 0 comment '当前关卡需要的次数',
`p_id` int(11) not null default 0 comment '奖品id',
`sort` int(11) not null default 1 comment '第几关',
`bind_show_sorts` varchar (256) not null default '' comment '绑定通关展示奖品的关卡sorts',
`open_question` tinyint(1)  not null default 0 comment '是否开启问题 0为关闭 1开启',
`icon` varchar (256) not null default '' comment '关卡图标',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`gl_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='游戏关卡';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_prize` (
`p_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`name` varchar (256) not null default '' comment '奖品名称',
`img` varchar (256) not null default '' comment '奖品图片',
`type` varchar (64)  not null default 'level' comment 'level  box',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`p_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='奖品表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_box` (
`gb_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`name` varchar (256) not null default '' comment '宝箱名称',
`open_tips` varchar (256) not null default '' comment '开宝箱提示语',
`p_id` int(11) not null default 0 comment '奖品id',
`friend_view` int(11) not null default 0 comment '好友到访次数',
`type` tinyint(1)  not null default 0 comment '是否共享 0 1共享',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`gb_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='游戏宝箱表';


CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_question` (
`q_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`question` varchar (256) not null default '' comment '问题描述',
`first` varchar (256) not null default '' comment '回答A',
`second` varchar (256) not null default '' comment '回答B',
`third` varchar (256) not null default '' comment '回答C',
`sure_method` int(11) not null default 1 comment '选项A1  选项b 2 选项C 3',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`q_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='宝箱问答集合';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_life_flag` (
`glf_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`uid` int(11) not null default 0 comment '用户uid',
`fromuid` int(11) not null default 0 comment '来自uid',
`icon` varchar (256) not null default '' comment '当前关卡宝箱的图片',
`tips` varchar (256) not null default '' comment '提示语',
`title` varchar (256) not null default '' comment '当前的关卡宝箱的标题',
`type` varchar (64)  not null default 'friend' comment 'friend  level  order  share  box',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`glf_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='成长记录表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_user_prize_flag` (
`up_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`uid` int(11) not null default 0 comment '用户uid',
`p_id` int(11) not null default 0 comment '奖品id',
`sort` int(11) not null default 0 comment '第几关',
`type` varchar (64)  not null default 'friend_view' comment 'level box',
`date` varchar (64)  not null default '0000-00-00' comment '年月日',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`up_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='用户奖品记录表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_user_level_flag` (
`gul_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`uid` int(11) not null default 0 comment '用户uid',
`gl_id` int(11) not null default 1 comment '游戏关卡id',
`status` varchar(64)  not null default 'process' comment 'process end',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`gul_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='用户游戏关卡记录';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_user_box_flag` (
`gub_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`uid` int(11) not null default 0 comment '用户uid',
`gb_id` int(11) not null default 1 comment '宝箱id',
`p_id` int(11) not null default 0 comment '奖品id',
`q_id` int(11) not null default 1 comment '回答问题id',
`remain_friend_view` int(11) not null default 0 comment '剩余好友到访次数',
`is_shares` tinyint(1)  not null default 0 comment '是否共享 0 1共享',
`is_end` tinyint(1)  not null default 1 comment '是否完成 0 1完成',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`gub_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='用户宝箱获取记录';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_game_level_question` (
`glq_id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) not null default 1 comment '对应的themeid',
`uid` int(11) not null default 0 comment '用户uid',
`gl_id` int(11) not null default 0 comment '关卡id',
`q_id` int(11) not null default 1 comment '回答问题id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`glq_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='游戏关卡问题记录表';
";
                db_query($createTable);
                yxlog("创建表：游戏表", '创建表');
            }


//异业多产品核销
            if ($input['mulit_products'] == 1 && ($system['mulit_products'] != 1 || empty($system))) {
                require_once get_path('plugin/sjmulitverify/core/install.php');
                yxlog("创建表：异业多产品表表", '创建表');
            }

            if ($input['iswechatgroup'] == 1 && ($system['iswechatgroup'] != 1 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_awechatgroup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`themeid` int(11) DEFAULT '0',
`workerid` int(11) DEFAULT '0',
`name` varchar(32) DEFAULT NULL,
`img` varchar(300) DEFAULT NULL,
`count` int(11) DEFAULT '0',
`addtime` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;   ";
                db_query($createTable);
                yxlog("创建表：群二维码", '创建表');
            }

//            判断快速配置为异业模式
            if ($input['softstyle'] >= 101 && ($system['softstyle'] < 101 || empty($system))) {
                $update_sql = "
ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` MODIFY COLUMN `address` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL AFTER `name`;
ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` ADD COLUMN `mappoint` text  comment '定位经纬度';
ALTER TABLE `yx_" . MODULE_NAME . "_ashangjia` ADD COLUMN `new_mobile` text comment '新手机号字段';
";
                db_query($update_sql);
                yxlog('更新商家表:地址、电话、定位字段', '创建表');
            }


            if ($input['noticeopen'] == 1 && ($system['noticeopen'] != 1 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_mbxxlog` (
`id` int(11) not null AUTO_INCREMENT,
`themeid` int(11) DEFAULT '0',
`uid` int(11) DEFAULT '0',
`template_id` int(11)  null   comment '模版id',
`message` json,
`sendtime` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='模版消息记录表';";

                db_query($createTable);
                yxlog("创建表：模版消息记录表", '创建表');
            }

            if ($input['ruanjian'] == 5 && ($system['ruanjian'] != 5 || empty($system))) {
                $createTable = "CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_bargain_entry` (
`be_id` int(11) not null AUTO_INCREMENT,
`order_id` int(11)  null   comment '订单id',
`remain_amount` DECIMAL(6,2)  null comment '当前砍至金额',
`end_time` timestamp default '0000-00-00 00:00:00' comment '砍价结束时间',
`status` tinyint(1) not null default 2 comment '砍价实例状态 1完成 2未完成 3已过期',
`time_type` varchar(24) not null default 'no_limit' comment '砍价时间显示 no_limit limit',
`show_grid` varchar(24) not null default 'no_show' comment '展示黄金宝刀 no_show 后台未开启 no未展示 yes已展示',
`show_share` varchar(24) not null default 'no_show' comment '展示分享 no_show 后台未开启 no未展示 yes已展示',
`show_share_double` varchar(24) not null default 'no_show' comment '展示二次分享 no_show 后台未开启 no未展示 yes已展示',
`bargain_gift` varchar(24) not null default 'close' comment 'close关闭 process进行中 success成功未兑换 receive成功已兑换 verification核销',
`bargain_gift_remain_num` int(11) not null default 0 comment '定制兑换礼品需邀请剩余人数',
`uid` int(11)  null  comment '发起人uid',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`be_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='订单砍价实体表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_bargain_flag` (
`b_id` int(11) not null AUTO_INCREMENT,
`uid`  int(64)  null  comment '助力人uid',
`avatar` varchar (300) not null default '' comment '助力人头像',
`order_id` varchar(64) not null default '' comment '助力订单id',
`help_type` varchar(64) not null default 'friend' comment '砍价类型 friend好友助力 system系统宝刀 share分销群、好友',
`be_id` int(11)  null comment '订单砍价实体id',
`sub_amount` DECIMAL(6,2)  null comment '助力金额',
`from_uid` int(11)  null  comment '发起人uid',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`b_id`)
) ENGINE=InnoDB AUTO_INCREMENT=107 DEFAULT CHARSET=utf8 COMMENT='砍价助力记录表';

CREATE TABLE IF NOT EXISTS `yx_" . MODULE_NAME . "_bargain_help_gift` (
`hg_id` int(11) not null AUTO_INCREMENT,
`uid` int(11)  null   comment '用户uid',
`g_id` int(11)  null   comment '礼品id',
`user_name` varchar(64)  not null default ''  comment '预留姓名',
`user_mobile` varchar(64)  not null default ''  comment '预留手机号',
`from_uid` int(11)  null   comment '帮助用户uid',
`g_name` varchar(64)  not null default ''  comment '礼品名称',
`g_img` varchar(512)  not null default ''  comment '礼品图片',
`status` tinyint(1)  not null default 1 comment '状态 0已领取未填写信息 1已填写信息,未核销 2已核销',
`themeid` int(11)  null  comment '活动id',
`created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
`updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
PRIMARY KEY (`hg_id`)
) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='砍价帮砍礼品记录';


";
                db_query($createTable);
                db_query("ALTER TABLE yx_" . MODULE_NAME . "_bargain_entry ADD COLUMN products_id int(2) default 0 ");
                db_query("ALTER TABLE yx_" . MODULE_NAME . "_arecord ADD COLUMN products_id int(2) default 0 ");
                db_query("ALTER TABLE yx_" . MODULE_NAME . "_arecord ADD COLUMN pn_wxapp int(2) default 0 ");
                yxlog("创建表：砍价记录表", '创建表');
            }

            if (DOMAIN1 == "www.yixuan.net" && $_G['mpid'] == 2) {
                $input['mchid'] = !empty($input['mchid']) ? $input['mchid'] : $system['mchid'];
                $input['apikey'] = !empty($input['apikey']) ? $input['apikey'] : $system['apikey'];
                $input['serial_no'] = !empty($input['serial_no']) ? $input['serial_no'] : $system['serial_no'];
            }
            $keys = ['appid', 'secret', 'mchid', 'apikey', 'apiclient_cert', 'apiclient_key', 'appidxcx', 'secretxcx', 'mchidxcx', 'apikeyxcx', 'apiclient_certxcx', 'apiclient_keyxcx', 'serial_no', 'serial_no_xcx'];
            foreach ($keys as $key) {
                if ($input[$key] != $system[$key]) {
                    $input[$key . '_old'] = $system[$key];
                } else {
                    $input[$key . '_old'] = $system[$key . '_old'];
                }
            }
            if ($_P['mpid'] != $_G['mpid']) {
                $res = Modules::UpdateMpid($themeid, $_P['mpid']);
            }

            $res = Modules::UpdateModulesByThemeid($themeid, ['system' => $input]);
            if ($res) {
                unset($system['s']);
                if ($system['adminname'] == $input['adminname']) {
                    unset($system['admintime']);
                    unset($input['admintime']);
                }
                writelog(0, '系统编辑', $system, $input, $_G['username']);
//$url = createWebUrl('theme', array('op' => 'display'));
                if ($is_clear_cache) {
                    $url = createWebUrl('clearcache', array('count' => 0, 'themeid' => $_P['themeid']));
                    return return_tips($tishi . '清空缓存中。。。', $url, 'success');
                } else {
                    return return_tips($tishi . '参数保存成功！', '', 'success');
                }
            }
        }
        if (empty($system['adminpass'])) {
            $system['autored'] = 1;
            $system['adminpass'] = 10000 + rand(0, 9999);
            $system['h5work'] = 1;
            if (empty($system['appid']) && empty($system['ruanjian'])) {
                $system['ruanjian'] = 2;
                if ($is_not_h5) {
                    $system['softmode'] = 1;
                }
            }
            if (empty($system['fenxiao'])) {
                $system['fenxiao'] = 0;
                $system['send_mode'] = 0;
            }
            if (empty($system['fenxiao01'])) {
                $system['fenxiao01'] = 0;
            }
            if (empty($system['softstyle'])) {
                $system['softstyle'] = 0;
            }

            if (empty($system['gongzhong'])) {
                if ($system['s']['domain1'] == "www.yixuan.net") {
                    $system['gongzhong'] = 1;
                    if ($_G['mpid'] == 2) {
                        $system['domainhead1'] = $system['s']['domain5'];
                        $system['domainhead3'] = $system['s']['domain6'];
                    }
                } else {
                    $system['gongzhong'] = 0;
                }
            }
            if (empty($system['tixing'])) {
                $system['tixing'] = 1;
                $system['noticeopen'] = 0;
            }
//            if(empty($system['wxapp']) && !empty($system['s']['wxapp'])) {
//                $system['s']['wxapp'] = explode(',',$system['s']['wxapp']);
//                $wxapp_index = intval( substr(MODULE_NAME, -1))%count($system['s']['wxapp']);
//                $system['wxapp'] = $system['s']['wxapp'][$wxapp_index];
//            }
        }
        if (empty($system['neworder'])) {
            $system['neworder'] = TIXING;
        }
        if (empty($system['version'])) {
            $system['version'] = $system['s']['version'];
        }
        if (empty($system['wechatgroupmax'])) {
            $system['wechatgroupmax'] = 200;
        }

        if (empty($system['appid']) || empty($system['secret']) || empty($system['mchid']) || empty($system['apikey'])) {
//            $payment = Modules::getPlatform($_G['mpid']);
//            $system['appid'] = $payment['appid'];
//            $system['secret'] = $payment['secret'];
//            if (DOMAIN1 != "www.yixuan.net" || $_G['mpid'] != 2) {
//                $softid = substr(MODULE_NAME, -5);
//                if ($softid % 10 == 1 && !empty($system['s']['mchid2']) && $_G['mpid'] == 2) {
//                    $system['mchid'] = $system['s']['mchid2'];
//                    $system['apikey'] = $system['s']['apikey2'];
//                } else {
//                    $system['mchid'] = $payment['mchid'];
//                    $system['apikey'] = $payment['apikey'];
//                    if (DOMAIN1 == "www.yixuan.net") {
//                        $system['serial_no'] = $payment['serial_no'];
//                        if ($_G['mpid'] == 2) {
//                            $payment['apiclient_cert'] = array();
//                        }
//                    }
//                    if (!empty($payment['apiclient_cert']) && !empty($payment['apiclient_key'])) {
//                        $system['apiclient_cert'] = self::write_pem($payment['apiclient_cert']);
//                        $system['apiclient_key'] = self::write_pem($payment['apiclient_key']);
//                    }
//                }
//            }
        }
        $PluginsList = self::outPutPluginsList($system);
        $return_clicent = get_defined_vars();
        // dump($_G);
        return view('/system', $return_clicent);
    }


    function write_pem($txt)
    {
        $path = ATTACHMENT_ROOT . 'cert/' . MODULE_NAME . '/';
        if (!is_dir($path)) {
            mkdirs($path);
        }
        $filename = generate_random_string() . '.pem';
        $filename = $path . $filename;
        $txt = authcode($txt, 'DECODE');
        $myfile = fopen($filename, "w") or die("Unable to open file!");
        fwrite($myfile, $txt);
        fclose($myfile);
        return $filename;
    }

    function outPutPluginsList($system)
    {
        $all_html = '';
        $all_list = [
            'lottery' => ['identity' => 'lottery', 'category' => 'biz', 'name' => '易宣新抽奖-九宫格抽奖', 'version' => '1.0', 'author' => 'cbw', 'status' => 0, 'thumb' => '', 'desc' => ''],
            'daiyan' => ['identity' => 'daiyan', 'category' => 'biz', 'name' => '代言模式', 'version' => '1.0', 'author' => 'cbw', 'status' => 0, 'thumb' => '', 'desc' => ''],
        ];
        foreach ($all_list as $k => $val) {
            $item_key = $val['identity'];
            $open_item = $system[$item_key] ? true : false;
            $item_html = '<div class="form-group">' . PHP_EOL . '<label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label">' . $val['name'] . '</label>' . PHP_EOL .
                '<div class="col-sm-8 col-xs-12">' . PHP_EOL;
            if ($open_item) {
                $item_html .= '<label class="radio-inline"><input type="radio" name="' . $val['identity'] . '" id="' . $val['identity'] . '" value="0" > 关闭' .
                    '</label>' . PHP_EOL . '<label class="radio-inline">' . PHP_EOL .
                    '<input type="radio" name="' . $item_key . '" id="' . $item_key . '" value="1" checked="checked"> 开启' . PHP_EOL . '</label>' . PHP_EOL;
            } else {
                $item_html .= '<label class="radio-inline"><input type="radio" name="' . $val['identity'] . '" id="' . $val['identity'] . '" value="0" checked="checked"> 关闭' .
                    '</label>' . PHP_EOL . '<label class="radio-inline">' . PHP_EOL .
                    '<input type="radio" name="' . $item_key . '" id="' . $item_key . '" value="1" > 开启' . PHP_EOL . '</label>' . PHP_EOL;
            }
            $item_html .= '</div>' . PHP_EOL . '</div>' . PHP_EOL;
            $all_html .= $item_html;
        }
        return $all_html;
    }


}